package com.neutech.shop.service.impl;

import com.neutech.shop.entity.User;
import com.neutech.shop.mapper.UserMapper;
import com.neutech.shop.service.UserService;
import com.neutech.shop.util.SsmUtil;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Neuedu-远哥
 * @version 1.0
 * @date 2021/9/22 9:52
 */
@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper mapper;
    @Override
    public List<User> selectAll() {
        return mapper.selectAll();
    }

    @Override
    public User selectById(Long id) {
        return mapper.selectById(id);
    }

    @Override
    public void register(User user, String hcity, String hproper, String harea, String addr2) {
        // 其他业务
        if(harea == null){
            harea = "";
        }
        String address = hcity + hproper + harea + addr2;
        user.setAddress(address);
        mapper.insert(user);
    }

    @Override
    public void delete(Long id) {
        mapper.delete(id);
    }

    @Override
    public void update(User user) {
        mapper.update(user);
    }

    @Override
    public void updateStatus(Long id) {
        // 根据id找User
        User user = selectById(id);
        if(user != null){
            // 先拿到user对象状态
            user.setStatus(user.getStatus() == 1 ? 0 : 1);
        }
        mapper.updateStatus(user);
    }

    @Override
    public User findByUsername(String username) {

        return mapper.findByUsername(username);
    }

    @Override
    public boolean login(User user) {
        String username = user.getUsername();
        // 数据表中的user对象
        User uServer = mapper.findByUsername(username);
        if(uServer != null && uServer.getPassword().equals(SsmUtil.stringToMD5(user.getPassword()))){
            return true;
        } else{
            return false;
        }
    }
}
